import request from '../../utils/request';

// IoT 设备信息接口
export interface DeviceVO {
  id?: number;
  deviceKey: string;
  deviceName: string;
  productId: number;
  productKey?: string;
  productName?: string;
  deviceType?: number;
  nickname?: string;
  gatewayId?: number;
  status?: number;
  statusLastUpdateTime?: Date;
  lastOnlineTime?: Date;
  lastOfflineTime?: Date;
  activeTime?: Date;
  createTime?: Date;
  ip?: string;
  firmwareVersion?: string;
  deviceSecret?: string;
  mqttClientId?: string;
  mqttUsername?: string;
  mqttPassword?: string;
  authType?: string;
  latitude?: number;
  longitude?: number;
  areaId?: number;
  address?: string;
  serialNumber?: string;
}

// 设备状态更新接口
export interface DeviceUpdateStatusVO {
  id: number;
  status: number;
}

// 设备分页查询参数
export interface DevicePageParams {
  pageNo?: number;
  pageSize?: number;
  deviceName?: string;
  deviceKey?: string;
  productId?: number;
  status?: number;
  deviceType?: number;
}

// 设备数据接口
export interface DeviceDataVO {
  id?: number;
  deviceId: number;
  deviceKey?: string;
  property: string;
  value: string;
  createTime?: Date;
}

// IoT 设备 API
export const DeviceApi = {
  // 查询设备分页
  getDevicePage: async (params: DevicePageParams) => {
    return await request.get('/iot/device/page', { params });
  },

  // 查询设备详情
  getDevice: async (id: number) => {
    return await request.get(`/iot/device/${id}`);
  },

  // 新增设备
  createDevice: async (data: DeviceVO) => {
    return await request.post('/iot/device', data);
  },

  // 修改设备
  updateDevice: async (data: DeviceVO) => {
    return await request.put('/iot/device', data);
  },

  // 删除设备
  deleteDevice: async (id: number) => {
    return await request.delete(`/iot/device/${id}`);
  },

  // 更新设备状态
  updateDeviceStatus: async (data: DeviceUpdateStatusVO) => {
    return await request.put('/iot/device/status', data);
  },

  // 获取设备数量统计
  getDeviceCount: async (productId?: number) => {
    return await request.get('/iot/device/count', { params: { productId } });
  },

  // 获取设备简单列表
  getDeviceSimpleList: async (productId?: number) => {
    return await request.get('/iot/device/simple-list', { params: { productId } });
  },

  // 获取设备数据
  getDeviceData: async (deviceId: number, property?: string) => {
    return await request.get('/iot/device/data', { params: { deviceId, property } });
  },

  // 发送设备指令
  sendDeviceCommand: async (deviceId: number, command: string, params?: any) => {
    return await request.post('/iot/device/command', { deviceId, command, params });
  },

  // 导出设备
  exportDevice: async (params: DevicePageParams) => {
    return await request.get('/iot/device/export', { params, responseType: 'blob' });
  }
};